<template>
  <div>
    <el-form class="form" label-width="120px">
      <el-form-item label="站点语言:">
        <el-select
          v-model="lang"
          placeholder="请选择站点语言"
          multiple
          @change="changLang"
        >
          <el-option
            v-for="item in option"
            :key="item.value"
            :label="item.title"
            :value="item.value"
            :disabled="item.status"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button class="save" type="primary" size="small" @click="save">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import langApi from '@/api/websetting'
export default {

  data() {
    return {
      option: [],
      lang: '',
      langed: [],
      langs: [],
      websiteId: ''
    }
  },
  computed: {
    siteId1() {
      return this.$store.getters.siteId
    }
  },
  watch: {
    siteId1(val, value) {
      this.websiteId = val

      this.getLangInfo()
    }
  },
  created() {
    // this.getLangSystem()

    this.websiteId = this.$store.getters.siteId

    this.getLangInfo()
  },
  methods: {
    changLang(val) {
      this.langs = val
    },
    //   系统支持语言
    getLangSystem() {
      langApi.systemLang().then(res => {
        const arr = Object.keys(res.data)
        const arr1 = Object.values(res.data)
        const arr2 = []
        for (let i = 0;i < arr.length;i++) {
          arr2.push({
            title: arr[i],
            value: arr1[i],
            status: false
          })
        }

        const arr3 = arr2
        arr3.forEach(el => {
          if (this.langed.indexOf(el.value) > -1) {
            el.status = true
          }
          return arr3
        })

        this.option = arr3
      })
    },
    // 语言详情
    getLangInfo() {
      langApi.langInfo({ site_id: this.websiteId }).then(res => {
        this.langed = []

        res.data.forEach(element => {
          this.langed.push(element.lang)
        })

        this.getLangSystem()
      })
    },
    // 语言添加
    save() {
      const val = {
        site_id: this.websiteId,
        lang: this.langs
      }
      langApi.langUpdate(val).then(res => {
        if (this.langs.length > 0) {
          if (res.code == 0) {
            this.$message.success(res.msg)
            this.getLangInfo()
          } else {
            this.$message.error(res.msg)
          }
        } else {
          this.$message.error('请选择语言后保存')
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.form{
    width: 95%;
    margin-top: 20px;
    .el-select{
        width: 100%;
    }
}
</style>
